/**
 * 具体的资源池信息
 */
// 领域查询的参数
var fieldValue = null;
// 地域查询的参数省
var provinceAreaValue = null;
// 地域查询的参数市
var cityAreaValue = null;
// 地域查询的参数区
var areaAreaValue = null;

$(function () {
    var htmlElement = {
        "xq": {
            "url": "/index/lib/Requirements",
            "infoUrl": "resource_xq_info.html",
            "titletext1": "军民科技协同创新需求池",
            "titletext2": "军民科技协同创新各类资源在这里汇聚",
            "destinationtext": "军民科技协同创新需求池"
        },
        //"cg": {
            //"url": "/index/lib/Harvests",
            //"infoUrl": "resource_cg_info.html",
            //"titletext1": "军民科技协同创新成果池",
            //"titletext2": "军民科技协同创新成果在这里汇聚",
            //"destinationtext": "军民科技协同创新成果池"
        //},
        "zj": {
            "url": "/index/lib/experts",
            "infoUrl": "resource_zj_info.html",
            "titletext1": "军民科技协同创新专家池",
            "titletext2": "军民科技协同创新专家资源在这里汇聚",
            "destinationtext": "军民科技协同创新专家池"
        },
        "ys": {
            "url": "/index/lib/Colleges",
            "infoUrl": "resource_ys_info.html",
            "titletext1": "军民科技协同创新院所池",
            "titletext2": "军民科技协同创新高校院所资源在这里汇聚",
            "destinationtext": "军民科技协同创新院所池"
        },
        //"qy": {
            //"url": "/index/lib/Enterprises",
            //"infoUrl": "resource_qy_info.html",
            //"titletext1": "军民科技协同创新企业池",
            //"titletext2": "军民科技协同创新各类资源在这里汇聚",
            //"destinationtext": "军民科技协同创新企业池"
        //},
        "fwjg": {
            "url": "/index/lib/Services",
            "infoUrl": "resource_fw_info.html",
            "titletext1": "军民科技协同创新服务池",
            "titletext2": "军民科技协同创新服务资源在这里汇聚",
            "destinationtext": "军民科技协同创新服务池"
        }
    };

    // 默认当前页面
    var initCurrPage = 1;
    // 默认每页大小
    var initPageSize = 8;
    // 全局变量：分页组件是否存在，如果存在就不需要初始化，如果不存在需要初始化
    var paginationFlag = false;
    // 获取url参数
    var urlParams = getQueryVariable();
    var urlFinal = null;
    var infoUrlFinal = null;
    // 重构页面
    rebuildHtml();
    // 类型
    var typeFinal = null;
    // 分页参数
    var paginationParamsFinal = null;
    // 获取资源领域
    getResdomain();
    // 初始化查询查询
    initSearch();

    /**
     * 重构页面
     */
    function rebuildHtml() {
        var type = urlParams["type"];
        var data = htmlElement[type];
        var titletext1 = data["titletext1"];
        var titletext2 = data["titletext2"];
        var destinationtext = data["destinationtext"];
        $("#title-text1").html(titletext1);
        $("#title-text2").html(titletext2);
        $("#destination-text").html(destinationtext);
        urlFinal = data["url"];
        infoUrlFinal = data["infoUrl"];
    }

    // 获取参数
    function getQueryVariable() {
        var url = window.location.search; //获取url中"?"符后的字串
        var paramsJson = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                paramsJson[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return paramsJson;
    }

    // 资源领域获得接口
    function getResdomain() {
        var type = urlParams["type"];
        let params = {
            url: "/index/resdomain",
            data: {type: type},
            sCallback: function (data) {
                getResdomainSuccessCallback(data)
            },
            eCallback: function (res) {
                $("#field-item").hide();
            }
        }
        requset(params);
    }

    // 资源领域成功的回调
    function getResdomainSuccessCallback(data) {
        var htmlCode = "";
        if (!data) {
            return
        }
        var dataList = data["所属领域"];
        var type = urlParams["type"];
        if(type == "fwjg"){
            dataList = data["机构类别"];
            htmlCode += "<option value='all'>-选择机构类别-</option>";
        } else if(type == "zj"){
            dataList = data["研究领域"];
            htmlCode += "<option value='all'>-选择研究领域-</option>";
        } else {
            htmlCode += "<option value='all'>-选择所属领域-</option>";
        }
        if (!dataList) {
            return;
        }
        $.each(dataList, function (index, value) {
            // 获取真实数据 {1: "智能装备"}
            $.each(value, function (key, value) {
                htmlCode += "<option value=" + key + ">" + value + "</option>";
            })
        });
        $("#field-item").html(htmlCode);
    }

    /**
     * 资源领域的下拉事件
     */
    $('#field-item').on('change', function () {
        var selectedKey = $(this).children('option:selected').val();
        if (selectedKey == "all") {
            fieldValue = null
        } else {
            fieldValue = selectedKey;
        }
    })

    // 查询数据
    function searchData(paramData) {
        if (!paramData) {
            paramData = {};
        }
        var areaValue = getAreaValue();
        paramData.objCountry = areaValue;
        paramData.objDomain = fieldValue;
        // 获取输入框值
        var objNameVal = $('#objName').val();
        if(objNameVal != null && objNameVal != ''){
            paramData.objName = objNameVal
        }
        paramData.loadAll = false;
        paramData.needPaging = true;
        paramData.currPage = paramData.currentPage ? paramData.currentPage : initCurrPage;
        paramData.pageSize = paramData.pageSize ? paramData.pageSize : initPageSize;

        let params = {
            url: urlFinal,
            data: paramData,
            sCallback: function (data) {
                searchDataSuccessCallback(data);
            },
            eCallback: function (res) {
            }
        }
        requset(params);
    }

    /**
     * 分页查询之后的成功回调
     * @param data
     */
    function searchDataSuccessCallback(data) {
        var total = data.total;
        var pageSize = data.pageSize;
        let paginationParams = {total: total, pageSize: pageSize};
        if (!paginationFlag) {
            paginationFlag = true;
            paginationInit(paginationParams, changePagination);
        }
        // 设置数据
        var htmlCode = "";
        var idArray = [];
        var type = urlParams["type"];
        $.each(data.rows, function (index, dataMap) {
            // 获取真实数据 {id: "305305a0-b701-11e8-8bb3-00163e0280a5", status: 1, name: "我是一家企业", country: "天津市,天津市区,和平区", reqName: "这是一个需求测试", …}
            var id = dataMap['id'];//id
            var content = "";
            var logo = dataMap['logo'];
            var name = dataMap['name'];//名称
            if (logo == null || logo == undefined || logo == '') {
                if(type="xq"){
                    logo = "images/xqlogo.png";
                } else if(type="cg"){
                    logo = "images/cglogo.png";
                } else if(type="fwjg"){
                    logo = "images/fwlogo.png";
                } else if(type="gx"){
                    logo = "images/gxlogo.png";
                } else if(type="qy"){
                    logo = "images/qylogo.png";
                } else if(type="zj"){
                    logo = "images/zjlogo.jpg";
                }

            } else {
                logo = imageUrl +logo;
            }
            if(type == "xq"){
                var reqName = dataMap['reqName'];//需求名称
                var domain = dataMap['domain'];//领域
                content = content + "<p>名称：" + name + "</p> ";
                content = content + "<p>需求名称：" + reqName + "</p>";
                content = content + "<p>领域：" + domain + "</p> ";
            } else if(type == "zj"){
                var workUnit = dataMap['workUnit'];//所在单位
                var researchField = dataMap['researchField'];//领域
                content = content + "<p>姓名：" + name + "</p> ";
                content = content + "<p>所在单位：" + workUnit + "</p>";
                content = content + "<p>研究领域：" + researchField + "</p> ";
                if (logo == null || logo == undefined || logo == '') {
                    logo = "images/expert.jpg";
                }
            } else if(type == "qy"){
                var country = dataMap['country'];//地区
                var domain = dataMap['domain'];//领域
                content = content + "<p>企业名：" + name + "</p> ";
                content = content + "<p>所属领域：" + domain + "</p> ";
                content = content + "<p>所在地区：" + country + "</p>";
                if (logo == null || logo == undefined || logo == '') {
                    logo = "images/enterprise.jpg";
                }
            } else if(type == "cg"){
                var resName = dataMap['resName'];//成果名称
                var domain = dataMap['domain'];//领域
                content = content + "<p>成果名称：" + resName + "</p>";
                content = content + "<p>成果完成单位：" + name + "</p> ";
                content = content + "<p>所属领域：" + domain + "</p> ";
                name = resName;
            } else if(type == "ys"){
                var number = dataMap['number'];//编号
                var country = dataMap['country'];//领域
                content = content + "<p>名称：" + name + "</p> ";
                content = content + "<p>所属地区：" + country + "</p> ";
                if (logo == null || logo == undefined || logo == '') {
                    logo = "images/college.jpg";
                }
            } else if(type == "fwjg"){
                var number = dataMap['number'];//编号
                var orgType = dataMap['orgType'];//机构性质
                content = content + "<p>名称：" + name + "</p> ";
                content = content + "<p>编号：" + number + "</p>";
                content = content + "<p>机构类别：" + orgType + "</p>";
            }
            htmlCode +=
                "<li style='padding-right:10px;padding-left: 10px' id=" + id + "> <a href='javascript:;'><img src=" + logo + "> <div class='desc'> "
                 +content+
                " </div> <h3>" + name + "</h3> </a> </li>";
            idArray.push(id);
        });
        $("#item-box").html(htmlCode);
        itemClickRegister(idArray)
    }

    /**
     * 为每一个card注册点击事件
     */
    function itemClickRegister(idArray) {
        var isLogin = checkLogin();
        $.each(idArray, function (index, value) {
            $('#' + value).on('click', function () {
                if(isLogin) {
                    window.location.href = infoUrlFinal + "?id=" + value;
                } else {
                    $("#showModal").modal('show');
                }
            })
        })
    }

    // 隐藏modal
    $("#confirmLogin").on("click", function () {
        $("#showModal").modal('hide');
    })

    /**
     * 分页组件发生改变之后回调
     * @param paginationParams
     */
    function changePagination(paginationParams) {
        paginationParamsFinal = paginationParams
        searchData(paginationParams)
    }

    /**
     * 构造省数据
     */
    function buildProvince() {
        var type = urlParams["type"];
        var provinceJson = getProvince();
        var htmlCode = "";
        htmlCode += "<option value='all'>----选择省----</option>";
        $.each(provinceJson, function (index, dataMap) {
            var key = dataMap["key"];
            var value = dataMap["value"];
            if (type == "cg" && value == "江苏省"){
                htmlCode += "<option value=" + key + " selected>" + value + "</option>";
                provinceAreaValue = key;
            } else {
                htmlCode += "<option value=" + key + " >" + value + "</option>";
            }
        })
        $("#region-province").html(htmlCode);
    }


    /**
     * 省的下拉事件
     */
    $('#region-province').on('change', function () {
        var selectedKey = $(this).children('option:selected').val();
        resetCity();
        resetArea();
        buildCity(selectedKey);
        if (selectedKey == "all") {
            provinceAreaValue = null
        } else {
            provinceAreaValue = selectedKey;
        }
    })

    /**
     * 重置市数据
     */
    function resetCity() {
        var htmlCode = "<option >----选择市----</option>";
        $("#region-city").html(htmlCode);
        cityAreaValue = null;
    }

    /**
     * 构造市数据
     */
    function buildCity(key) {
        var json = getChildren(key);
        var htmlCode = "";
        htmlCode += "<option value='all'>----选择市----</option>";
        $.each(json, function (index, dataMap) {
            var key = dataMap["key"];
            var value = dataMap["value"];
            htmlCode += "<option value=" + key + ">" + value + "</option>";
        })
        $("#region-city").html(htmlCode);
    }

    /**
     * 市的下拉事件
     */
    $('#region-city').on('change', function () {
        var selectedKey = $(this).children('option:selected').val();
        resetArea();
        buildArea(selectedKey);
        if (selectedKey == "all") {
            cityAreaValue = null
        } else {
            cityAreaValue = selectedKey;
        }
    })


    /**
     * 重置区数据
     */
    function resetArea() {
        var htmlCode = "<option>----选择区----</option>";
        $("#region-area").html(htmlCode);
        areaAreaValue = null;
    }

    /**
     * 构造区数据
     */
    function buildArea(key) {
        var json = getChildren(key);
        var htmlCode = "";
        htmlCode += "<option value='all'>----选择区----</option>";
        $.each(json, function (index, dataMap) {
            var key = dataMap["key"];
            var value = dataMap["value"];
            htmlCode += "<option value=" + key + ">" + value + "</option>";
        })
        $("#region-area").html(htmlCode);
    }

    /**
     * 区的下拉事件
     */
    $('#region-area').on('change', function () {
        var selectedKey = $(this).children('option:selected').val();
        if (selectedKey == "all") {
            areaAreaValue = null
        } else {
            areaAreaValue = selectedKey;
        }
    })

    function getAreaValue() {
        if (provinceAreaValue == null) {
            return null;
        } else {
            if (cityAreaValue == null) {
                return provinceAreaValue;
            } else {
                if (areaAreaValue == null) {
                    return cityAreaValue;
                } else {
                    return areaAreaValue;
                }
            }
        }
    }


    /**
     * 查询事件
     */
    $("#search-btn").click(function () {
        paginationFlag = false;
        searchData(paginationParamsFinal);
    });


    function initSearch(){
        // 构造省下拉框
        buildProvince();
        var type = urlParams["type"];
        if (type == "cg") {
            buildCity(provinceAreaValue);
        }
        searchData();
    }
});


